package InterviewTest.q0301_TripleInOne;

public class TripleInOne {
    // 直接用一个数组模拟三个栈
    // 只需要定三个指针 每次出入栈移动指针并更新值即可
    int[] stacks;
    int size;
    int p1, p2, p3;
    public TripleInOne(int stackSize) {
        stacks = new int[stackSize * 3];
        size = stackSize;
        p1 = 0;
        p2 = stackSize;
        p3 = 2 * stackSize;

    }

    public void push(int stackNum, int value) {
        if (stackNum == 0) {
            if (p1 < size) {
                stacks[p1] = value;
                p1++;
            }
        }else if (stackNum == 1) {
            if (p2 < 2 * size) {
                stacks[p2] = value;
                p2++;
            }
        }else {
            if (p3 < 3 * size) {
                stacks[p3] = value;
                p3++;
            }
        }
    }

    public int pop(int stackNum) {
        if (stackNum == 0) {
            if (p1 > 0) {
                return stacks[--p1];
            }
        }else if (stackNum == 1) {
            if (p2 > size) {
                return stacks[--p2];
            }
        }else {
            if (p3 > 2 * size) {
                return stacks[--p3];
            }
        }

        return -1;
    }

    public int peek(int stackNum) {
        if (stackNum == 0) {
            if (p1 > 0) {
                return stacks[p1 - 1];
            }
        }else if (stackNum == 1) {
            if (p2 > size) {
                return stacks[p2 - 1];
            }
        }else {
            if (p3 > 2 * size) {
                return stacks[p3 - 1];
            }
        }

        return -1;
    }

    public boolean isEmpty(int stackNum) {
        if (stackNum == 0) {
            if (p1 > 0) {
                return false;
            }
        }else if (stackNum == 1) {
            if (p2 > size) {
                return false;
            }
        }else {
            if (p3 > 2 * size) {
                return false;
            }
        }

        return true;
    }
}
